package training.minggu5.maven.ui.penjualan;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import training.minggu5.maven.domain.PenjualanDetail;
import training.minggu5.maven.domain.PenjualanHeader;
import training.minggu5.maven.service.PenjualanDetailService;
import training.minggu5.maven.service.PenjualanHeaderService;


public class ShowPenjualan extends HttpServlet {

    private PenjualanHeaderService headvice;
    private PenjualanDetailService detvice;

    @Override
    public void init(ServletConfig config) throws ServletException {
	ApplicationContext ctx = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext());

	headvice = (PenjualanHeaderService) ctx.getBean("penjualanHeaderService");
        detvice = (PenjualanDetailService) ctx.getBean("penjualanDetailService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<PenjualanHeader> listhead = headvice.findAllPenjualanHeader();

        resp.setContentType("text/html");
        String output = "<html>";
        output+="<head>";
        output+="<title>";
        output+="halaman muka";
        output+="</title>";
        output+="</head>";
        output+="<body>";
        output+="<h1>list penjualan:</h1>";
        for(PenjualanHeader lishet : listhead){
            output+="nomor faktur  : "+lishet.getNoFaktur()+"<br>";
            output+="tanggal       : "+lishet.getTanggal()+"<br>";
            output+="nama konsumen : "+lishet.getNama_konsumen()+"<br>";
            output+="detail        : <br>";
            List<PenjualanDetail> listdet = detvice.findPenjualanDetailByNoFaktur(lishet.getNoFaktur());
            for(PenjualanDetail lisde : listdet){
                output+="\tid             : "+lisde.getId_penjualan_detail()+"<br>";
                output+="\tkode barang    : "+lisde.getBarang().getKodeBarang()+"<br>";
                output+="\tnama barang    : "+lisde.getBarang().getNamaBarang()+"<br>";
                output+="\tunit price     : "+lisde.getBarang().getUnitPrice()+"<br>";
                output+="\tjumlah terjual : "+lisde.getJumlah()+"<br>";
                output+="\ttotal harga    : "+lisde.getHarga()+"<br>";
            }
            output+="<a href='editPenjualan?k="+lishet.getNoFaktur()+"'>edit</a> | ";
            output+="<a href='deletePenjualan?k="+lishet.getNoFaktur()+"'>delete</a><br><br>";
        }
        output+="<a href='insertPenjualan'>simpan penjualan baru</a></body>";
        output+="</html>";
        resp.getWriter().print(output);
        resp.getWriter().close();

    }

}
